<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xiaobai
 * Date: 13-5-24
 * Time: 上午10:18
 * To change this template use File | Settings | File Templates.
 */
class Proxy_model extends CI_Model {

    private $_write_db;
    function __construct()
    {
        parent::__construct();
        $this->load->library('error_code_library');
        $this->_write_db = $this->load->database('master', TRUE);
    }

    public function get_proxy_ip() {
        $this->db->select('id, ip, port');
        $this->db->from('proxy');
        $this->db->where('status', 1);
        $this->db->where('vtime > 0');
        $this->db->order_by('vtime DESC, use_count');
        //$this->db->limit(100);
        $query = $this->db->get();

        if($query->num_rows() <= 0) {
            return $this->error_code_library->getError('system', 'data', 'no_data', 'In Function '. __FUNCTION__);
        } else {
            $result_array = $query->result_array();
            $rand_key = array_rand($result_array);
            return  $result_array[$rand_key];
        }
    }

    public function update_proxy_ip_use_count($id = NULL) {
        if(is_null($id)) {
            return $this->error_code_library->getError('system', 'params', 'no_must_parameter', 'ID In Function '. __FUNCTION__);
        } else {
            $this->_write_db->set('use_count', 'use_count+1', FALSE);
            $this->_write_db->where('`id` = '.$id);
            $result = $this->_write_db->update('proxy');
        }
    }

    public function update_proxy_ip_vtime($id = NULL) {
        if(is_null($id)) {
            return $this->error_code_library->getError('system', 'params', 'no_must_parameter', 'ID In Function '. __FUNCTION__);
        } else {
            $this->_write_db->set('vtime', time());
            $this->_write_db->where('`id` = '.$id);
            $result = $this->_write_db->update('proxy');
        }

    }

    public function label_proxy_ip($ip = array()) {
        if(count($ip) < 1) {
            return $this->error_code_library->getError('system', 'params', 'no_must_parameter', 'ID In Function '. __FUNCTION__);
        } else {
            $this->_write_db->set('status', 0);
            $this->_write_db->where('`id` = '.$ip['id']);
            $result = $this->_write_db->update('proxy');
        }
    }

    public function get_proxy_status() {
        //select COUNT(id),status from proxy WHERE vtime IS NOT NULL GROUP BY status
        $this->db->select('COUNT(id) AS NUM , status');
        $this->db->from('proxy');
        $this->db->where('vtime > 0');
        $this->db->group_by('status');
        $this->db->order_by('status DESC');
        $query = $this->db->get();
        //echo $this->db->last_query();
        if($query->num_rows() <= 0) {
            $statistics = NULL;
            return $statistics;
        } else {
            $statistics['all'] = $query->result_array();
        }
        $this->db->select('COUNT(id) AS NUM , status');
        $this->db->from('proxy');
        $this->db->where('ctime >', (time() - 60));
        $this->db->where('vtime > 0');
        $this->db->group_by('status');
        $this->db->order_by('status DESC');
        $query = $this->db->get();
        //echo $this->db->last_query();
        if($query->num_rows() <= 0) {
            $statistics['new'] = NULL;
            return $statistics;
        }else{
            $statistics['new'] = $query->result_array();
            return $statistics;
        }
    }
}